package cn.edu.gdut.deepcode.controller;


import cn.edu.gdut.deepcode.domain.po.Result;
import cn.edu.gdut.deepcode.service.ICanvasSharedService;
import cn.edu.gdut.deepcode.utils.CommonUtils;
import cn.edu.gdut.deepcode.context.UserContext;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.edu.gdut.deepcode.constant.AbnormalInfoConstant.CS_EI_2;
import static cn.edu.gdut.deepcode.constant.ReqRespConstant.AUTHORIZATION_HEADER_NAME;

/**
 * <p>
 * 画布分享表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-04-11
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/canvas-shares")
public class CanvasSharedController {

    private final ICanvasSharedService canvasSharedService;

    /**
     * <p>
     *      分享码生成
     * </p>
     *
     * @Author: myc
     * @Since: 2025-04-13  18:02
     * @Version: 1.0.0
     */
    @ApiOperation(value = "分享码生成接口",notes = "响应体中的data属性为分享码")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "JWT 令牌",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @GetMapping("/{canvasId}/{expire}")
    public Result<String> genSharedCode(
            @ApiParam("画布id") @PathVariable("canvasId")Long canvasId,
            @ApiParam("分享码的过期时间,单位为分钟")@PathVariable("expire") Long expire
            ){
        return canvasSharedService.genSharedCode(canvasId,expire);
    }

    /**
     * <p>
     *      分享码校验并保存画布基础信息
     * </p>
     *
     * @Author: myc
     * @Since: 2025-04-13  18:02
     * @Version: 1.0.0
     */
    @ApiOperation(value = "分享码校验并保存画布基础信息接口",notes = "响应体中的data属性为canvasId")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "画布分享码",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @PostMapping("/{sharedCode}")
    public Result<?> checkSharedCodeAndGetCanvas(
            @ApiParam("画布分享码") @PathVariable("sharedCode") String sharedCode
            ) {
        if (StrUtil.isBlank(sharedCode)){
            CommonUtils.recordErrorInfoAndThrowException(
                    CS_EI_2,
                    List.of(UserContext.getUser(),sharedCode)
            );
        }
        return canvasSharedService.checkSharedCodeAndGetCanvas(sharedCode);
    }

    /**
     * <p>
     *      分享码销毁
     * </p>
     *
     * @Author: myc
     * @Since: 2025-04-13  18:02
     * @Version: 1.0.0
     */
    @ApiOperation(value = "取消分享接口",notes = "响应体中的data属性为null")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "JWT 令牌",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @DeleteMapping("/{canvasId}")
    public Result<?> cancelSharedCode(
            @ApiParam("画布id") @PathVariable("canvasId") Long canvasId){
        return canvasSharedService.cancelSharing(canvasId);
    }

    @ApiOperation(value = "根据canvasId获取分享码",notes = "响应体中的data属性为分享码")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "JWT 令牌",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @GetMapping("/{canvasId}")
    public Result<String> getSharedCodeByCanvasId(
            @ApiParam("画布id") @PathVariable("canvasId") Long canvasId)  {
        return canvasSharedService.getSharedCodeByCanvasId(canvasId);
    }


}
